//
//  TVRechargeViewController.swift
//  StarTimesON
//
//  Created by Toj on 12/2/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit
import TTCOM_iOS

class TVRechargeViewController: STBaseViewController {

    private let buyButtonHeight: CGFloat = 96.ui_scale
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        headerView.frame = CGRect(x: 50, y: CGFloat.nav_barFullHeight, width: .sr_width-50, height: headerViewHeight)
        
        view.addSubview(collectionView)
        view.addSubview(headerView)
        collectionView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(view)
            make.top.equalTo(CGFloat.nav_barFullHeight)
        }
    }
    
    private let headerViewHeight: CGFloat = 118
    private lazy var headerView: TVRechargeHeaderView = {
        let view = TVRechargeHeaderView()
        view.rightView.addTarget(self, action: #selector(didTapCardQuestion), for: .touchUpInside)
        return view
    }()
    
    @objc private func didTapCardQuestion() {
//        print("-->toj: range:\(headerView.cardIDTextField.textField.selectedTextRange)")
        
        
        let alert = STCouponAlert()
        alert.layer.zPosition = 999
        view.addSubview(alert)
        alert.frame = view.bounds
        
        if let frontVC = parent as? FrontViewController {
            frontVC.navigationBar.bringSubviewToFront(alert)
        }
    }
    
    private let footerInset = 96.ui_scale
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.bounces = false
        view.scrollsToTop = true
        view.alwaysBounceVertical = true
        
        view.showsVerticalScrollIndicator = false
        view.backgroundColor = .white
        view.contentInset = UIEdgeInsets(top: headerViewHeight, left: 0, bottom: 0, right: 0)
        view.contentInsetAdjustmentBehavior = .never
        
        view.register(UICollectionViewCell.cell_class, forCellWithReuseIdentifier: UICollectionViewCell.cell_identifier)
        
        view.dataSource = self
        view.delegate = self
        return view
    }()

    deinit {
        print("-->toj: STTVCommodityController: deinit")
        NotificationCenter.default.removeObserver(self)
    }
}


extension TVRechargeViewController: UICollectionViewDataSource {
   
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 30
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UICollectionViewCell.cell_identifier, for: indexPath)
        cell.contentView.backgroundColor = .random
        return cell
    }
}

extension TVRechargeViewController: UICollectionViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y
        print("-->toj: offsetY:\(offsetY)")
        
        if offsetY > 0 && (-headerView.frame.origin.y > headerViewHeight)  {
            return
        }
        
        if -offsetY > headerViewHeight {
            let y = (-offsetY - headerViewHeight)
            var frame = headerView.frame
            frame.origin.y = y + CGFloat.nav_barFullHeight
            headerView.frame = frame
            return
        }
        
        let y = (offsetY + headerViewHeight)
        if y >= 0 {
            var frame = headerView.frame
            frame.origin.y = -y + CGFloat.nav_barFullHeight
            headerView.frame = frame
            return
        }
    }
}

extension TVRechargeViewController: UICollectionViewDelegateFlowLayout {
    
    // cell 大小
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return .def_cipher
    }
}
